﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Forums.Domain.Values
{
	using System;
	using System.Collections.Generic;
	using System.Web.Mvc;
	using Falcon.Core;
	using Falcon.Core.Domain;
	using Falcon.Core.Domain.Repositories;
	using Falcon.Core.Domain.Values;
	using Falcon.Core.Extensions;
	using Falcon.Core.Validation;
	using Falcon.Forums.Domain.Enums;

	public class ForumPostCreate : ValueBase<ForumPostCreate>
	{
		// private fields
		private Forum _forum;
		private IList<AttachmentStore> _attachments;

		// private constants
		private const string QuoteFormat = "[quote user={0}]{1}[/quote]";

		/// <summary>
		/// Initializes a new instance of the <see cref="ForumPostCreate"/> class.
		/// </summary>
		public ForumPostCreate()
		{
			this.AttachmentStoreRepository = Application.Resolve<IAttachmentStoreRepository>();

			var identity = User.Identity;

			if (identity == null)
			{
				return;
			}

			this.Attachment = new AttachmentCreate
			{
				Bind = ForumsSettings.Posts.AttachmentFieldKey,
				Group = ForumsSettings.Posts.AttachmentKey,
				TempId = identity.Id,
				UserId = identity.Id
			};

			this.Attachments = this.AttachmentStoreRepository.GetAll(new AttachmentStoreSpecification
			{
				Group = this.Attachment.Group,
				Key = this.Attachment.TempId.ToString()
			});
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="ForumPostCreate"/> class.
		/// </summary>
		/// <param name="forum">The forum.</param>
		/// <param name="parent">The parent.</param>
		/// <param name="quote">if set to <c>true</c> [quote].</param>
		public ForumPostCreate(Forum forum, ForumPost parent, bool quote) : this()
		{
			this.TopicId = parent.TopicId;
			this.ParentId = parent.Id;
			this.Title = ForumsResources.Titles.Re.Fmt(parent.Title);
			this.Forum = forum;

			if (quote)
			{
				this.Body = QuoteFormat.Fmt(parent.User.Name, parent.Body);
			}
		}

		/// <summary>
		/// Gets or sets the attachment store repository.
		/// </summary>
		/// <value>The attachment store repository.</value>
		public IAttachmentStoreRepository AttachmentStoreRepository
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the forum id.
		/// </summary>
		/// <value>The forum id.</value>
		public int ForumId
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the topic id.
		/// </summary>
		/// <value>The topic id.</value>
		public int TopicId
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the parent id.
		/// </summary>
		/// <value>The parent id.</value>
		public int ParentId
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the title.
		/// </summary>
		/// <value>The title.</value>
		public string Title
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the body.
		/// </summary>
		/// <value>The body.</value>
		public string Body
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the notification.
		/// </summary>
		/// <value>The notification.</value>
		public ForumTopicSubscriptionNotification Notification
		{
			get;
			set;
		}

		/// <summary>
		/// Gets the notifications.
		/// </summary>
		/// <value>The notifications.</value>
		public SelectList Notifications
		{
			get
			{
				return new SelectList(Enum.GetNames(typeof(ForumTopicSubscriptionNotification)), this.Notification.Name());
			}
		}

		/// <summary>
		/// Gets or sets the attachment.
		/// </summary>
		/// <value>The attachment.</value>
		public AttachmentCreate Attachment
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the topic.
		/// </summary>
		/// <value>The topic.</value>
		public Forum Forum
		{
			get
			{
				return _forum;
			}
			set
			{
				_forum = value;

				if (_forum == null)
				{
					return;
				}

				this.ForumId = _forum.Id;
			}
		}

		/// <summary>
		/// Gets or sets the attachments.
		/// </summary>
		/// <value>The attachments.</value>
		public IList<AttachmentStore> Attachments
		{
			get
			{
				if (_attachments == null)
				{
					_attachments = new List<AttachmentStore>();
				}

				return _attachments;
			}
			set
			{
				_attachments = value;
			}
		}

		/// <summary>
		/// Validation rules for this instance.
		/// </summary>
		/// <returns></returns>
		public override Validator<ForumPostCreate> Rules()
		{
			var rules = base.Rules();

			rules.Add(x => x.Title)
				.NotEmpty(Resources.Errors.Required.Fmt(Resources.Titles.Title));

			rules.Add(x => x.Body)
				.NotEmpty(Resources.Errors.Required.Fmt(Resources.Titles.Message));

			return rules;
		}

		/// <summary>
		/// Validates this instance.
		/// </summary>
		public override void Validate()
		{
			this.Rules().Validate(this);
		}
	}
}